package com.fa4j.system.login.listener;

import com.fa4j.system.account.model.Account;
import com.fa4j.system.account.model.AccountEvent;
import com.fa4j.system.account.model.AccountPasswordModifyEvent;
import com.fa4j.system.account.model.AccountPasswordRestEvent;
import com.fa4j.system.login.service.LoginSessionService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AccountListener {

    private final LoginSessionService loginSessionService;

    @EventListener
    public void accountModify(AccountEvent.Modify event) {
        Account account = event.afterData();
        loginSessionService.refreshSession(account.getId());
    }

    @EventListener
    public void accountPwdModify(AccountPasswordModifyEvent event) {
        loginSessionService.removeAccountSession(event.getAccountId());
    }

    @EventListener
    public void accountPwdRestModify(AccountPasswordRestEvent event) {
        loginSessionService.removeAccountSession(event.getAccountId());
    }
}
